home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Cream of the Crop 26
/
Cream of the Crop 26.iso
/
program
/
sockdem.zip
/
_SETUP.1
/
socketsDlg.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1997-06-19
|
10KB
|
384 lines
// socketsDlg.cpp : implementation file
//
#include "stdafx.h"
#include "sockets.h"
#include "socketsDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSocketsDlg dialog
CSocketsDlg::CSocketsDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSocketsDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSocketsDlg)
m_ed3 = _T("");
m_ed4 = _T("");
m_RemotePort = 0;
m_LocalPort = 0;
m_rxText = _T("");
m_txText = _T("");
m_localIP = _T("");
m_remoteIP = _T("");
m_state = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CSocketsDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSocketsDlg)
DDX_Text(pDX, IDC_EDIT3, m_ed3);
DDX_Text(pDX, IDC_EDIT4, m_ed4);
DDX_Control(pDX, IDC_WINSOCK1, m_mysock);
DDX_Text(pDX, IDC_EDIT1, m_RemotePort);
DDV_MinMaxLong(pDX, m_RemotePort, 0, 65535);
DDX_Text(pDX, IDC_EDIT2, m_LocalPort);
DDV_MinMaxLong(pDX, m_LocalPort, 0, 65535);
DDX_Text(pDX, IDC_EdRX, m_rxText);
DDX_Text(pDX, IDC_EdTX, m_txText);
DDX_Text(pDX, IDC_EDIT5, m_localIP);
DDX_Text(pDX, IDC_EDIT6, m_remoteIP);
DDX_Text(pDX, IDC_STATE, m_state);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSocketsDlg, CDialog)
//{{AFX_MSG_MAP(CSocketsDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnSetSocket)
ON_EN_CHANGE(IDC_EdTX, OnChangeEdTX)
ON_WM_TIMER()
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_Send, OnSend)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSocketsDlg message handlers
BOOL CSocketsDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
pMySocket =(CMSWinsockControl*) GetDlgItem (IDC_WINSOCK1);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
pMySocket->SetProtocol( (long) 1);
m_ed4=pMySocket->GetLocalHostName();
m_state=pMySocket->GetState();
VariantInit(&myVar);
myVar.bstrVal=SysAllocString(L"Hello world.");
//start the timer which updaes the STATE field.
myTimer=SetTimer(1,100,NULL);
UpdateData(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
void CSocketsDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CSocketsDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CSocketsDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
//Here is the code to set the SOCKET properties !
void CSocketsDlg::OnSetSocket()
{
//Get data from form INTO member vars.
UpdateData(TRUE);
//Set REMOTE host by name
pMySocket->SetRemoteHost(m_ed3);
//Set REMOTE port from Long member variable.
pMySocket->SetRemotePort(m_RemotePort);
//Set LOCAL port from Long member variable
pMySocket->SetLocalPort(m_LocalPort);
//get IP addresses
m_remoteIP =pMySocket->GetRemoteHostIP();
m_localIP =pMySocket->GetLocalIP();
m_RemotePort=pMySocket->GetRemotePort();
m_LocalPort=pMySocket->GetLocalPort();
m_ed4=pMySocket->GetLocalHostName();
//Set data from member varianles onto form.
UpdateData(FALSE);
//attempt a send.
myVar.vt=VT_BSTR;
myVar.bstrVal=SysAllocString(L"Hello world.");
pMySocket->SendData(myVar);
//Set data from member varianles onto form.
UpdateData(FALSE);
}
void CSocketsDlg::OnChangeEdTX()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function to send the EM_SETEVENTMASK message to the control
// with the ENM_CHANGE flag ORed into the lParam mask.
// TODO: Add your control notification handler code here
}
BEGIN_EVENTSINK_MAP(CSocketsDlg, CDialog)
//{{AFX_EVENTSINK_MAP(CSocketsDlg)
ON_EVENT(CSocketsDlg, IDC_WINSOCK1, 0 /* DataArrival */, OnDataArrivalWinsock1, VTS_I4)
ON_EVENT(CSocketsDlg, IDC_WINSOCK1, 6 /* Error */, OnErrorWinsock1, VTS_I2 VTS_PBSTR VTS_I4 VTS_BSTR VTS_BSTR VTS_I4 VTS_PBOOL)
ON_EVENT(CSocketsDlg, IDC_WINSOCK1, 1 /* Connect */, OnConnectWinsock1, VTS_NONE)
ON_EVENT(CSocketsDlg, IDC_WINSOCK1, 2 /* ConnectionRequest */, OnConnectionRequestWinsock1, VTS_I4)
ON_EVENT(CSocketsDlg, IDC_WINSOCK1, 5 /* Close */, OnCloseWinsock1, VTS_NONE)
ON_EVENT(CSocketsDlg, IDC_WINSOCK1, 3 /* SendProgress */, OnSendProgressWinsock1, VTS_I4 VTS_I4)
ON_EVENT(CSocketsDlg, IDC_WINSOCK1, 4 /* SendComplete */, OnSendCompleteWinsock1, VTS_NONE)
//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()
void CSocketsDlg::OnDataArrivalWinsock1(long bytesTotal)
{
// TODO: Add your control notification handler code here
char* myString2="These bytes arrived 9999999999999999999999";
VARIANT myData;
VariantInit(&myData);
VARIANT myTypeOfVar;
VariantInit(&myTypeOfVar);
myTypeOfVar.vt=VT_I2;
myTypeOfVar.iVal=VT_BSTR;
VARIANT myMaxLen;
VariantInit(&myMaxLen);
myMaxLen.vt=VT_I2;
myMaxLen.iVal=999;
sprintf(myString2,"These bytes arrived %d ",bytesTotal);
myData.vt = 8;
pMySocket->GetData( &myData, myTypeOfVar , myMaxLen );
m_txText = m_txText + V_BSTR(&myData);
UpdateData(FALSE);
}
void CSocketsDlg::OnErrorWinsock1(short Number, BSTR FAR* Description, long Scode, LPCTSTR Source, LPCTSTR HelpFile, long HelpContext, BOOL FAR* CancelDisplay)
{
// TODO: Add your control notification handler code here
MessageBox("An error follows:-");
MessageBox((const char *)Description);
}
void CSocketsDlg::OnConnectWinsock1()
{
// TODO: Add your control notification handler code here
MessageBox("OnConnect");
}
void CSocketsDlg::OnConnectionRequestWinsock1(long requestID)
{
// TODO: Add your control notification handler code here
MessageBox("OnConnectionRequest");
}
void CSocketsDlg::OnCloseWinsock1()
{
// TODO: Add your control notification handler code here
MessageBox("OnClose");
}
void CSocketsDlg::OnSendProgressWinsock1(long bytesSent, long bytesRemaining)
{
// TODO: Add your control notification handler code here
MessageBox("OnSendProgress");
}
void CSocketsDlg::OnSendCompleteWinsock1()
{
// TODO: Add your control notification handler code here
MessageBox("OnSendComplete");
}
void CSocketsDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
UpdateData(TRUE);
//get port state
m_state=pMySocket->GetState();
//get IP addresses and stuff
m_remoteIP =pMySocket->GetRemoteHostIP();
m_localIP =pMySocket->GetLocalIP();
m_ed4=pMySocket->GetLocalHostName();
UpdateData(FALSE);
CDialog::OnTimer(nIDEvent);
}
void CSocketsDlg::OnDestroy()
{
KillTimer(myTimer);
pMySocket->Close();
CDialog::OnDestroy();
}
void CSocketsDlg::OnSend()
{
// TODO: Add your control notification handler code here
//attempt to send some data if the port is open.
if(pMySocket->GetState() == 1)
{
myVar.vt=VT_BSTR;
myVar.bstrVal=SysAllocString(L"Hello world.");
pMySocket->SendData(myVar);
}
else
{
MessageBox("Cannot send, port is not open.");
}
}